// 登录功能实现
const loginBtn = document.getElementById('loginBtn');
const usernameInput = document.getElementById('username');
const passwordInput = document.getElementById('password');

// 初始化
function init() {
    // 绑定登录按钮事件
    loginBtn.addEventListener('click', handleLogin);
    
    // 绑定回车键登录
    usernameInput.addEventListener('keypress', handleKeyPress);
    passwordInput.addEventListener('keypress', handleKeyPress);
    
    // 检查是否有保存的用户信息
    checkSavedCredentials();
}

// 处理登录逻辑
function handleLogin() {
    const username = usernameInput.value.trim();
    const password = passwordInput.value.trim();
    
    // 基本验证
    if (!username || !password) {
        showNotification('请输入用户名和密码', 'error');
        return;
    }
    
    // 模拟登录验证（实际项目中应该发送到服务器）
    if (username === 'admin' && password === 'admin123') {
        // 保存登录状态到本地存储
        localStorage.setItem('crmLoggedIn', 'true');
        localStorage.setItem('crmUsername', username);
        
        // 如果选择了记住密码，保存密码（实际项目中应该加密）
        const rememberCheckbox = document.querySelector('.remember-me input');
        if (rememberCheckbox.checked) {
            localStorage.setItem('crmRemember', 'true');
            localStorage.setItem('crmPassword', password);
        } else {
            localStorage.removeItem('crmRemember');
            localStorage.removeItem('crmPassword');
        }
        
        showNotification('登录成功，正在跳转...', 'success');
        
        // 跳转到主页面
        setTimeout(() => {
            window.location.href = 'main.html';
        }, 1000);
    } else {
        showNotification('用户名或密码错误', 'error');
    }
}

// 处理回车键登录
function handleKeyPress(e) {
    if (e.key === 'Enter') {
        handleLogin();
    }
}

// 检查保存的凭据
function checkSavedCredentials() {
    const remember = localStorage.getItem('crmRemember');
    if (remember === 'true') {
        const savedUsername = localStorage.getItem('crmUsername');
        const savedPassword = localStorage.getItem('crmPassword');
        
        if (savedUsername && savedPassword) {
            usernameInput.value = savedUsername;
            passwordInput.value = savedPassword;
            document.querySelector('.remember-me input').checked = true;
        }
    }
}

// 显示通知提示
function showNotification(message, type = 'info') {
    // 创建通知元素
    const notification = document.createElement('div');
    notification.className = `notification ${type}`;
    notification.textContent = message;
    
    // 设置样式
    Object.assign(notification.style, {
        position: 'fixed',
        top: '20px',
        right: '-400px',
        padding: '15px 25px',
        borderRadius: '6px',
        color: 'white',
        fontWeight: '500',
        zIndex: '9999',
        transition: 'right 0.3s ease',
        boxShadow: '0 5px 15px rgba(0, 0, 0, 0.2)'
    });
    
    // 根据类型设置背景色
    if (type === 'success') {
        notification.style.backgroundColor = '#28a745';
    } else if (type === 'error') {
        notification.style.backgroundColor = '#dc3545';
    } else {
        notification.style.backgroundColor = '#17a2b8';
    }
    
    // 添加到页面
    document.body.appendChild(notification);
    
    // 显示通知
    setTimeout(() => {
        notification.style.right = '20px';
    }, 100);
    
    // 自动隐藏
    setTimeout(() => {
        notification.style.right = '-400px';
        setTimeout(() => {
            notification.remove();
        }, 300);
    }, 3000);
}

// 页面加载完成后初始化
window.addEventListener('DOMContentLoaded', init);